<?php

namespace app\api\service\jd;
use app\api\model\Account;
use app\api\model\event\Video;
use app\api\model\jd\User;
use app\lib\exception\jd\JdException;
use think\Db;
use think\Exception;


class UserService
{
    private static $instance;

    public static function getInstance(): UserService
    {
        if (!self::$instance instanceof self) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function getCookie($params){
        $app_id = $params["app_id"];
        $cookie = Account::where("app_id",$app_id)->field("cookie")->find();
        $data = [
            "cookie" => $cookie->cookie,
            "app_id" => intval($app_id)
        ];
        return $data;
    }

    public function getInfo(){
        $info = User::where("status",1)->limit(1)->find();
        if (time() > strtotime($info["pub_time"])){
            return $info;
        }else{
            return "无数据";
        }
    }


    public function distribute($params){

        DB::startTrans();
        try {
           $data = [
               "product_url" => $params["product_url"],
               "status" => $params["status"],
               "reason" => $params["reason"],
               "app_id" => $params["app_id"],
               "draw_time" => date("YmdHis",time())
           ];
           User::where("video_id",$params["id"])->update($data);
           Db::commit();
        }catch (Exception $e){
            // 回滚事务
            Db::rollback();
            throw new JdException([
                'msg' => '分发失败',
                'error_code' => 20002
            ]);
        }
    }

}
